package studentsystem;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        ArrayList<Student> list=new ArrayList<>();
        loop:while (true){
            System.out.println("-------------欢迎来到黑马学生管理系统----------------");
            System.out.println("1：添加学生");
            System.out.println("2：删除学生");
            System.out.println("3：修改学生");
            System.out.println("4：查询学生");
            System.out.println("5：退出");
            System.out.printf("请输入您的选择：");
            switch (sc.next()) {
                case "1":
                    addStudent(list);
                    break;
                case "2":
                    deleteStudent(list);
                    break;
                case "3":
                    updateStudent(list);
                    break;
                case "4":
                    queryStudent(list);
                    break;
                case "5":
                    break loop;
                default:
                    System.out.println("没有这个选项！");
            }
        }
    }
    //添加学生
    public static void addStudent(ArrayList<Student> list){
        Scanner sc=new Scanner(System.in);
        Student stu=new Student();
        while(true){
            System.out.println("-------------开始添加学生信息----------------");
            System.out.printf("请输入学生id：");
            stu.setId(sc.next());
            if(contains(list,stu.getId())){
                System.out.println("！！此id已存在，请重新输入！！");
            }else{
                break;
            }
        }
        System.out.printf("请输入学生姓名：");
        stu.setName(sc.next());
        System.out.printf("请输入学生年龄：");
        stu.setAge(sc.nextInt());
        System.out.printf("请输入学生家庭住址：");
        stu.setAddress(sc.next());
        list.add(stu);
        System.out.println("-------------添加成功----------------");
    }
    //删除学生
    public static void deleteStudent(ArrayList<Student> list){
        Scanner sc=new Scanner(System.in);
        System.out.println("-------------开始进行删除操作----------------");
        System.out.printf("请输入要删除学生信息的id：");
        String delid=sc.next();
        if(contains(list,delid)){
            list.remove(getIndex(list,delid));
            System.out.println("-------------"+delid+"已删除----------------");
        }else{
            System.out.println("！！此学生id不存在！！");
        }
    }
    //修改学生
    public static void updateStudent(ArrayList<Student> list){
        Scanner sc=new Scanner(System.in);
        System.out.println("-------------开始进行修改操作----------------");
        System.out.printf("请输入要修改学生信息的id：");
        String exid=sc.next();
        int index=getIndex(list,exid);
        if(index>=0){
            System.out.println("将学生姓名修改为(输入-1则表示不改)：");
            String newName=sc.next();
            if(!newName.equals("-1")){
                list.get(index).setName(newName);
            }
            System.out.println("将学生年龄修改为(输入-1则表示不改)：");
            int newAge=sc.nextInt();
            if(newAge!=-1){
                list.get(index).setAge(newAge);
            }
            System.out.println("将学生家庭住址修改为(输入-1则表示不改)：");
            String newAddress=sc.next();
            if(!newAddress.equals("-1")){
                list.get(index).setAddress(newAddress);
            }
            System.out.println("-------------"+exid+"已修改完毕----------------");
        }else{
            System.out.println("！！此学生id不存在！！");
        }
    }
    //查询学生
    public static void queryStudent(ArrayList<Student> list){
        if(list.size()==0){
            System.out.println("当前无学生信息，请添加后再查询");
        }else{
            //打印表头
            System.out.println("-------------学生信息----------------");
            System.out.println("id\t\t\t姓名\t\t年龄\t\t家庭住址");
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i).getId()+"\t\t"+list.get(i).getName()+"\t"+list.get(i).getAge()+"\t\t\t"+list.get(i).getAddress() );
            }
        }
    }
    //判断学生id是否存在
    public static boolean contains(ArrayList<Student> list,String id){
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId().equals(id)){
                return true;
            }
        }
        return false;
    }
    //通过id获取索引
    public static int getIndex(ArrayList<Student> list,String id){
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId().equals(id)){
                return i;
            }
        }
        return -1;
    }
}
